#include <string.h>
#include <iostream>
using namespace std;

/*
  栈 
  s->top++ 和 --s->top

  s->top++ 取s的top值,然后top--
  --s->top 先s--,然后在取s的top值


 */

typedef int Item;
typedef struct stack_type* Stack;

struct stack_type{
  Item *contents;
  int top; //栈顶 
  int size;
}; 

Stack create(int size);

bool is_stack_empty(Stack s);

bool is_full(Stack s);

void push(Stack s,Item i);

Item pop(Stack s);


int main(){

  Stack st = create(2);
  push(st,20);
  push(st,40);
  push(st,50);
  cout << "栈顶位置: " << st->top << endl;
  cout << "出栈: " << pop(st) << endl;
  cout << "出栈: " << pop(st) << endl;
  cout << "出栈: " << pop(st) << endl;


  return 0;
}


Stack create(int size){
  
  Stack s = (Stack)malloc(sizeof(struct stack_type));
  //给数组分配空间
  s->contents = (Item *)malloc(sizeof(Item) * size);
  s->size = size;
  s->top = 0;

  return s;
}

bool is_full(Stack s){
  return s->top == s->size;
}

bool is_stack_empty(Stack s){
  return s->top == 0;
}

void push(Stack s,Item i){
  if(is_full(s)){
    cout << "栈满了,不能在添加了!"<< endl;
    return;
  }

  s->contents[s->top++] = i;
}


Item pop(Stack s){
  if(is_stack_empty(s)){
    cout << "栈空了,不能在取了!"<< endl;
    return 0;
  }

  return s->contents[--s->top];
}







